(function (win) {
    //环境判断 prod 生产环境 test测试环境 dev开发环境
    var environment = function () {
        var host = location.href.match(/(http[s]?\:\/\/)(.*?)\//);
        var hostNames = ''
        var requestAdress = ''
        if (host && host[2].indexOf('localhost:') > -1) {
            hostNames = 'dev'
            requestAdress = host[0]
        } else if (host && host[2].indexOf('47.103.143.153') > -1) {
        //||host && host[2].indexOf('testeast.cathaylife.cn') > -1
            hostNames = 'test'
            requestAdress = host[0]
        }else{
            hostNames = ''
            requestAdress = host[0]
        }
        return {
            hostNames: hostNames,
            requestAdress: requestAdress
        }
    }();

    //当前正在请求的数量
    var needLoadingRequestCount = 0;
    var loadingHtml = '<div  style="width: 100%;height: 100%;position: fixed;z-index: 9999;left: 0;top:0"></div><div class="el-loading-spinner" style="background-color: gainsboro;width: 100px;padding: 15px;border-radius: 7px;position: absolute;left: 50%;top: 50%;transform: translate(-50%, -50%);z-index: 999;"><svg viewBox="25 25 50 50" class="circular"> <circle cx="50" cy="50" r="20" fill="none" class="path"></circle> </svg> <p class="el-loading-text">Loading...</p></div><div style="width: 100%;height: 100%;background: white;position: fixed;left: 0;top: 0;opacity: 0;"></div></div>';
    //显示loading
    function showLoading() {
        if (needLoadingRequestCount === 0) {
            var body = document.body;
            var loadingDiv = document.createElement('div')
            loadingDiv.id = 'east-loading-wrap'
            loadingDiv.innerHTML = loadingHtml;
            body.insertBefore(loadingDiv, document.getElementById('#pc'));
        }
        needLoadingRequestCount++;
    }

    //隐藏loading
    function hideLoading() {
        needLoadingRequestCount--;
        needLoadingRequestCount = Math.max(needLoadingRequestCount, 0); //做个保护
        if (needLoadingRequestCount === 0) {
            document.body.removeChild(document.getElementById('east-loading-wrap'))
        }
    }
    var Request = axios.create({
        //10.20.51.188 测试环境
        baseURL: 'http://192.168.43.67:8080/ygst-api/',
        // baseURL: 'http://47.103.143.153/ygst-api/',
        //baseURL:'http://localhost:8080',//本地localhost
        //baseURL:environment.requestAdress,//动态配置
        timeout: 5000,
        //headers: {
        //    token: document.cookie
        //}
    })
    Request.interceptors.request.use(function (config) {
//            config.headers['xxxx'] = token;
        if (config.headers.showLoading !== false) {
            showLoading();
        }
        return config;
    }, function (error) {
        //hideLoading()
        return error;
    })
    Request.interceptors.response.use(function (response) {
        if (response.config.headers.showLoading !== false) {
            hideLoading();
        }
        return response;
    }, function (err) {
        if (err.config.headers.showLoading !== false) {
            hideLoading();
        }
        //return err;
    })
    win._axios = Request
})(window)
